package com.xq.jltx.kotlinandroid

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.Looper
import android.util.ArrayMap
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV
import com.xq.jltx.kotlinandroid.mmkv.PreferenceHolder
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    val kv by lazy {
        if (MMKV.getRootDir().isNullOrEmpty()) {
            MMKV.initialize(this.application)
            println("mmkv root: ${MMKV.getRootDir()}")
        }
        MMKV.defaultMMKV()
    }
    private val sp by lazy {
        application.getSharedPreferences("ceshi", Context.MODE_PRIVATE)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn.setOnClickListener {
//            testSp()
            testStr()
        }
        btn1.setOnClickListener { testSp1() }
        btn2.setOnClickListener { testMmkv() }
//        btn2.setOnClickListener { testArrayMap() }
    }

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    private fun testArrayMap() {
        val map = ArrayMap<Int, String>()

        for (i in 0 until 20) {
            map.put(i, "value ${i}")
        }
    }
    private fun testStr() {
        var obj : Any? = null
        Log.d(TAG,"a" + "b" + obj)
        Log.d(TAG,"ab${obj}")
    }

    private fun testSp() {
        var first = PreferencesHelper.boolVal
        Log.d(TAG, "first.1$first")
        PreferencesHelper.boolVal = !first
        first = PreferencesHelper.boolVal
        Log.d(TAG, "first.2$first")

        var second = PreferencesHelper.intVal
        Log.d(TAG, "second.1$second")
        PreferencesHelper.intVal = second * 2
        second = PreferencesHelper.intVal
        Log.d(TAG, "second.2$second")


        var three = PreferencesHelper.longVal
        Log.d(TAG, "three.1$three")
        PreferencesHelper.longVal = three * 2
        three = PreferencesHelper.longVal
        Log.d(TAG, "three.2$three")


        var four = PreferencesHelper.floatVal
        Log.d(TAG, "four.1$four")
        PreferencesHelper.floatVal = four * 2
        four = PreferencesHelper.floatVal
        Log.d(TAG, "four.2$four")

        var five = PreferencesHelper.stringVal
        Log.d(TAG, "five.1$five")
        PreferencesHelper.stringVal = "${five}_$five"
        five = PreferencesHelper.stringVal
        Log.d(TAG, "five.2$five")

        var six = PreferencesHelper.objVal
        Log.d(TAG, "six.1${six.id}_${six.name}")
        PreferencesHelper.objVal = User(six.id * 2, six.name)
        six = PreferencesHelper.objVal
        Log.d(TAG, "six.2$six")
        six = SettingsPreferencesDelegate.getSP(
            "obj_val",
            User(112344, "hhhh"), User::class.java
        )
        Log.d(TAG, "six.3$six")

        SettingsPreferencesDelegate.setSP("ceshi", "我来了")
        val ceshi = SettingsPreferencesDelegate.getSP("ceshi", "来撩")
        Log.d(TAG, "ceshi.2$ceshi")

        var nameList = listOf(
            "jltx",
            "aidenquan"
        )
        Log.d(TAG, "nameList.1$nameList")
        SettingsPreferencesDelegate.setSP("list_val", nameList)
        nameList = SettingsPreferencesDelegate.getSP(
            "list_val",
            emptyList(), object : TypeToken<List<String>>() {}.type
        )

        Log.d(TAG, "nameList.2$nameList")
        SettingsPreferencesDelegate.clearSP()
    }

    private fun testSp1() {
        sp.edit().putInt("sp0", 1).commit()
        Log.d(TAG, "sp0:${sp.getInt("sp0",0)}")

    }
    private fun testMmkv1() {
        kv.putInt("kv0", 1)
        Log.d(TAG, "kv0:${kv.getInt("kv0",0)}")
    }

    private fun testMmkv() {
        var first = PreferenceHolder.boolVal
        Log.d(TAG, "first.1$first")
        PreferenceHolder.boolVal = !first
        Log.d(TAG, "first.2${PreferenceHolder.boolVal}")
    }


    companion object {
        private const val TAG = "HHHHH"
    }

}
